package com.zc.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zc.dao.impl.ArticleDaoImpl;
import com.zc.dao.impl.UserDaoImpl;
import com.zc.model.Article;
import com.zc.model.User;

public class LoginAction extends ActionSupport implements ModelDriven<User> {

	private static final long serialVersionUID = 1L;
	private User user = new User();

	@Override
	public String execute() throws Exception {
		System.out.println(user.toString());
		UserDaoImpl dao = new UserDaoImpl();
		User u = dao.getUser(user);
		if (u != null) {
			/**
			 * 如果登录成功，则从数据库中查询所有的文章集合
			 */
			ArticleDaoImpl articleDao = new ArticleDaoImpl();
			List<Article> articleList = articleDao.getAllArticle();
			ActionContext context = ActionContext.getContext();

			Map<String, Object> session = context.getSession();
			session.put("articles", articleList);
			session.put("user", u);
			System.out.println("找到user");
			return SUCCESS;
		} else {
			System.out.println("未找到user");
			return ERROR;
		}

		// System.out.println(user.getUsername());
		// System.out.println(user.getPassword());
		// return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public User getModel() {
		// System.out.println(user.toString());
		return this.user;
	}
}
